package hd.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * String工具集
 * <p/>
 * Created by Haidy on 14/11/9.
 */
public class StringUtils {

    public static boolean isEmail(String email) {
//        String str = "\\w+@(\\w+.)+[a-z]{2,3}";

        String str = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(email);
        return m.matches();
    }

    /**
     * 以友好的方式显示时间
     *
     * @param date 日期
     * @return 友好日期
     */
    public static String friendlyTime(String date) {
        Date time = TimeUtil.toDate(date);
        if (time == null)
            return "Unknown";
        String f_time = "";
        Calendar cal = Calendar.getInstance();

        // 判断是否是同一天
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String curDate = format.format(cal.getTime());
        String paramDate = format.format(time);
        if (curDate.equals(paramDate)) {
            int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
            if (hour == 0)
                f_time = Math.max((cal.getTimeInMillis() - time.getTime()) / 60000, 1) + "分钟前";
            else
                f_time = hour + "小时前";
            return f_time;
        }

        long lt = time.getTime() / 86400000;
        long ct = cal.getTimeInMillis() / 86400000;
        int days = (int) (ct - lt);
        if (days == 0) {
            int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
            if (hour == 0)
                f_time = Math.max((cal.getTimeInMillis() - time.getTime()) / 60000, 1) + "分钟前";
            else
                f_time = hour + "小时前";
        } else if (days == 1) {
            f_time = "昨天";
        } else if (days == 2) {
            f_time = "前天";
        } else if (days > 2 && days <= 10) {
            f_time = days + "天前";
        } else if (days > 10) {
            f_time = format.format(time);
        }
        return f_time;
    }

    /**
     * 获取某个字符出现的次数
     *
     * @param src  源
     * @param find 寻找Str
     * @return count
     */
    public static int getOccur(String src, String find) {
        int o = 0;
        int index = -1;
        while ((index = src.indexOf(find, index)) > -1) {
            ++index;
            ++o;
        }
        return o;
    }

    public static String inputStreamToString(InputStream is, String encoding) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] bytes = new byte[1024];
        int l;
        try {
            while ((l = is.read(bytes)) != -1) {
                byteArrayOutputStream.write(bytes, 0, l);
            }
            return byteArrayOutputStream.toString(encoding);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 字符串是否为空
     *
     * @param str string
     * @return 是否为空
     */
    public static boolean isEmpty(String str) {
        return str == null || str.isEmpty();
    }
}